package com.yyk.yykywd.mvvm.ui.fragment

import android.graphics.Typeface
import android.view.View
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.orhanobut.logger.Logger
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import com.yyk.yykywd.*
import com.yyk.yykywd.base.BaseFragment
import com.yyk.yykywd.mvvm.adapter.AgentAdapter
import com.yyk.yykywd.mvvm.entity.HomeSearchBean
import com.yyk.yykywd.mvvm.ui.SearchActivity
import com.yyk.yykywd.mvvm.view_model.CustomerViewModel
import com.yyk.yykywd.util.ToAnotherActivity
import com.yyk.yykywd.util.ToastUtil
import kotlinx.android.synthetic.main.activity_search.tv_search
import kotlinx.android.synthetic.main.fragment_new_customer.*
import kotlinx.android.synthetic.main.layout_load.*

/**
 * 文件描述：
 * Created by guo on 2019-11-25.
 */
class NewCustomerFragment : BaseFragment() {
    private lateinit var viewModel: CustomerViewModel
    private lateinit var agentAdapter: AgentAdapter

    companion object {
        const val TAG = "NewCustomerFragment"
    }

    override fun initData() {
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_new_customer
    }

    override fun initView() {
        viewModel = getFragmentViewModel(this, CustomerViewModel::class.java)
        setAgentLayout()

        agentAdapter = AgentAdapter(requireActivity(),
                R.layout.item_agent,
                viewModel.list)
        rv_agent.layoutManager = LinearLayoutManager(requireActivity())
        rv_agent.adapter = agentAdapter
    }

    override fun setListener() {
        super.setListener()
        tv_search.setOnClickListener {
            ToAnotherActivity.toCommonActivity(requireActivity(), SearchActivity::class.java)
        }
        ll_agent.setOnClickListener {
            setAgentLayout()
            reloadData()
        }
        ll_hospital.setOnClickListener {
            setHospitalLayout()
            reloadData()
        }
        ll_open_sea.setOnClickListener {
            setOpenSeaLayout()
            reloadData()
        }
        rl_potential_client.setOnClickListener {
            selected1()
            reloadData()
        }
        rl_c_client.setOnClickListener {
            selected2()
            reloadData()
        }
        rl_sign_contract_client.setOnClickListener {
            selected3()
            reloadData()
        }
        rl_return_money_complete.setOnClickListener {
            selected4()
            reloadData()
        }
        srl.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                viewModel.loadMore()
                viewModel.getAgentCustomer()
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                reloadData()
            }
        })

        viewModel.getListSuccess.observe(this, Observer {
            hideRefreshAndLoadMore(srl)
            showStateLayout(agentAdapter, it.code, viewModel.list, it.msg, loading_layout)
        })
        viewModel.getOpenSeaNumSuccess.observe(this, Observer {
            tv_open_sea_num.text = "（" + it.data.num + "）"
        })

        agentAdapter.setOnItemClickListener { _, _, position ->
            if (viewModel.from == OPEN_SEA) {
                if (whetherHavePermission()) {
                    return@setOnItemClickListener
                }
                if (viewModel.list[position].client_type == 2) {
                    ToAnotherActivity.toReturnMoneyDetailActivity(requireActivity(),
                            FROM_CUSTOMER_OPEN_SEA_AGENT,
                            viewModel.list[position].id.toString(), 0, 0, 0)
                } else {
                    ToAnotherActivity.toReturnMoneyDetailActivity(requireActivity(),
                            FROM_CUSTOMER_OPEN_SEA_HOSPITAL,
                            viewModel.list[position].id.toString(), 0, 0, 0)
                }

            } else if (viewModel.from == AGENT) {
                val typeS = viewModel.list[position].type_s
                when (viewModel.type) {
                    "1" -> {
                        ToAnotherActivity.toReturnMoneyDetailActivity(requireActivity(),
                                FROM_POTENTIAL_CUSTOMER_AGENT,
                                viewModel.list[position].id.toString(), 0, 0, typeS ?: 0)
                    }
                    "2" -> {
                        ToAnotherActivity.toReturnMoneyDetailActivity(requireActivity(),
                                FROM_C_CUSTOMER_AGENT,
                                viewModel.list[position].id.toString(), 0, 0, typeS ?: 0)
                    }
                    "3" -> {
                        ToAnotherActivity.toDetailActivity(requireActivity(),
                                FROM_SIGN_CONTRACT_AGENT,
                                viewModel.list[position].id.toString(), 0)
                    }
                    else -> {
                        ToAnotherActivity.toDetailActivity(requireActivity(),
                                FROM_CUSTOMER_RETURN_MONEY_AGENT,
                                viewModel.list[position].id.toString(), 0)
                    }
                }

            } else {
                val s = when (viewModel.type) {
                    "1" -> {
                        FROM_POTENTIAL_CUSTOMER_HOSPITAL
                    }
                    "2" -> {
                        FROM_C_CUSTOMER_HOSPITAL
                    }
                    "3" -> {
                        FROM_SIGN_CONTRACT_HOSPITAL
                    }
                    else -> {
                        FROM_INSTALL_HOSPITAL
                    }
                }
                ToAnotherActivity.toReturnMoneyDetailActivity(requireActivity(),
                        s,
                        viewModel.list[position].id.toString(), 0, 0, 0)
            }

        }
    }

    private fun setAgentLayout() {
        tv_agent.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
        tv_agent.textSize = 16f
        tv_agent_line.visibility = View.VISIBLE

        tv_hospital.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
        tv_hospital.textSize = 14f
        tv_hospital_line.visibility = View.GONE

        tv_open_sea.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
        tv_open_sea.textSize = 14f
        tv_open_sea_line.visibility = View.GONE

        ll_classify.visibility = View.VISIBLE

        tv_return_money_complete.text = "回款完成客户"
        viewModel.from = AGENT
        selected1()
    }

    private fun setHospitalLayout() {
        tv_hospital.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
        tv_hospital.textSize = 16f
        tv_hospital_line.visibility = View.VISIBLE

        tv_agent.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
        tv_agent.textSize = 14f
        tv_agent_line.visibility = View.GONE

        tv_open_sea.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
        tv_open_sea.textSize = 14f
        tv_open_sea_line.visibility = View.GONE

        ll_classify.visibility = View.VISIBLE
        tv_potential_client.setBackgroundResource(R.drawable.shape_4dp4)
        tv_potential_client.setTextColor(ContextCompat.getColor(requireActivity(), R.color.col_5987ff))

        tv_return_money_complete.text = "安装医院"
        viewModel.from = HOSPITAL
        viewModel.type = "1"
        selected1()
    }

    private fun setOpenSeaLayout() {
        tv_open_sea.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
        tv_open_sea.textSize = 16f
        tv_open_sea_line.visibility = View.VISIBLE

        tv_agent.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
        tv_agent.textSize = 14f
        tv_agent_line.visibility = View.GONE

        tv_hospital.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
        tv_hospital.textSize = 14f
        tv_hospital_line.visibility = View.GONE

        ll_classify.visibility = View.GONE

        viewModel.from = OPEN_SEA
        viewModel.layoutType = OPEN_SEA_LAYOUT
    }

    private fun selected1() {
        tv_potential_client.setBackgroundResource(R.drawable.shape_4dp4)
        tv_potential_client.setTextColor(ContextCompat.getColor(requireActivity(), R.color.col_5987ff))

        tv_c_client.setBackgroundResource(android.R.color.transparent)
        tv_c_client.setTextColor(ContextCompat.getColor(requireActivity(), R.color.col_808080))

        tv_sign_contract_client.setBackgroundResource(android.R.color.transparent)
        tv_sign_contract_client.setTextColor(ContextCompat.getColor(requireActivity(), R.color.col_808080))

        tv_return_money_complete.setBackgroundResource(android.R.color.transparent)
        tv_return_money_complete.setTextColor(ContextCompat.getColor(requireActivity(), R.color.col_808080))

        viewModel.type = "1"

        with(viewModel) {
            layoutType = if (from == AGENT) {
                AGENT_LAYOUT
            } else {
                HOSPITAL_LAYOUT
            }
        }
    }

    private fun selected2() {
        tv_c_client.setBackgroundResource(R.drawable.shape_4dp4)
        tv_c_client.setTextColor(ContextCompat.getColor(requireActivity(), R.color.col_5987ff))

        tv_potential_client.setBackgroundResource(android.R.color.transparent)
        tv_potential_client.setTextColor(ContextCompat.getColor(requireActivity(), R.color.col_808080))

        tv_sign_contract_client.setBackgroundResource(android.R.color.transparent)
        tv_sign_contract_client.setTextColor(ContextCompat.getColor(requireActivity(), R.color.col_808080))

        tv_return_money_complete.setBackgroundResource(android.R.color.transparent)
        tv_return_money_complete.setTextColor(ContextCompat.getColor(requireActivity(), R.color.col_808080))

        viewModel.type = "2"

        with(viewModel) {
            layoutType = if (from == AGENT) {
                AGENT_LAYOUT
            } else {
                HOSPITAL_LAYOUT
            }
        }
    }

    private fun selected3() {
        tv_sign_contract_client.setBackgroundResource(R.drawable.shape_4dp4)
        tv_sign_contract_client.setTextColor(ContextCompat.getColor(requireActivity(), R.color.col_5987ff))

        tv_c_client.setBackgroundResource(android.R.color.transparent)
        tv_c_client.setTextColor(ContextCompat.getColor(requireActivity(), R.color.col_808080))

        tv_potential_client.setBackgroundResource(android.R.color.transparent)
        tv_potential_client.setTextColor(ContextCompat.getColor(requireActivity(), R.color.col_808080))

        tv_return_money_complete.setBackgroundResource(android.R.color.transparent)
        tv_return_money_complete.setTextColor(ContextCompat.getColor(requireActivity(), R.color.col_808080))

        viewModel.type = "3"

        with(viewModel) {
            layoutType = if (from == AGENT) {
                AGENT_LAYOUT
            } else {
                HOSPITAL_LAYOUT
            }
        }
    }

    private fun selected4() {
        tv_return_money_complete.setBackgroundResource(R.drawable.shape_4dp4)
        tv_return_money_complete.setTextColor(ContextCompat.getColor(requireActivity(), R.color.col_5987ff))

        tv_c_client.setBackgroundResource(android.R.color.transparent)
        tv_c_client.setTextColor(ContextCompat.getColor(requireActivity(), R.color.col_808080))

        tv_potential_client.setBackgroundResource(android.R.color.transparent)
        tv_potential_client.setTextColor(ContextCompat.getColor(requireActivity(), R.color.col_808080))

        tv_sign_contract_client.setBackgroundResource(android.R.color.transparent)
        tv_sign_contract_client.setTextColor(ContextCompat.getColor(requireActivity(), R.color.col_808080))

        viewModel.type = "4"

        with(viewModel) {
            layoutType = if (from == AGENT) {
                AGENT_LAYOUT
            } else {
                CONTRACT_LAYOUT
            }
        }
    }

    private fun reloadData() {
        with(viewModel) {
            loading_layout.showLoading()
            agentAdapter.setFrom(from)
            agentAdapter.setCustomerType(type)
            agentAdapter.setLayoutType(layoutType)
            refresh(list)
            getAgentCustomer()
            getOpenSeaNum()
        }
    }

    override fun interfaceIsVisible() {
        super.interfaceIsVisible()
        reloadData()
    }
}